<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>获取请求</h1>
    <form action="" method="" id="form">
        <input type="text" id="ipn">
        <button id="btn">查询信息</button>
    </form> 


    <script>
        //获取元素
        const oBtn = document.getElementById('btn')
        const oForm = document.getElementById('form')
        const oIpt = document.getElementById('ipn')

        // 绑定表单事件
        oForm.onsubmit = function(){
            //获取输入的值
            const userId = oIpt.value;
            //实例化
            const xhr = new XMLHttpRequest();
            //打开路径
            //三个参数：1.请求类型   2.请求地址   3.是否用异步
            xhr.open('GET',`/hospital?userId=${userId}&_=${Date.now()}`,true);
            //发送请求
            xhr.send();
            //绑定事件
            xhr.onreadystatechange = function(){
            
            // 判断状态和响应是否成功
                if(xhr.readyState === 4 && (xhr.status >=200 && xhr.status <=299)){
                    //获取成功的数据
                    const data = JSON.parse(xhr.responseText);
                    console.log(data);


                }


            }
            return false


        }
    </script>
</body>
</html>